$(function(){
	var masks;
	$.ajax({
	   type: "POST",
	   url: ctx + '/web/car/products',
	   beforeSend: function(){
		   masks = $.maskWeb();
	   },
	   data: {openid:$('#openid').val()},
	   success: function(data){
		   if(data.success){
			   var html = '';
			   if(data.data.length > 0){
				   $(data.data).each(function(i,obj){
					   html += setCount(obj);
				   });
			   }else{
				   html = setNullContent();
			   }
			   $('.news-list').html(html);
			   $('#should').text(countTotalPrice());
			   $('#totalPrice').text(countTotalPrice());
		   }else{
			   $.toastWeb({message:data.message});
		   }
		   clickEvent();
	   },
	   complete:function(){
		   $.unmaskWeb(masks);
	   }
	});
});
function clickEvent(){
	/**
	 * 数量减
	 */
	$('.c_subtract').click(function(){
		var count = parseInt($(this).parent().find('input').val());
		var result = count-1;
		var li = $(this).parents('li.list-item');
		var productId = $(li).attr('productId');
		var stock = parseInt($(li).find('.stock').text());
		if(stock >= count){
			if(count > 1){
				if(changeCount(productId,result)){
					$(this).parent().find('input').val((result));
				}
			}
		}else{
			result = stock;
			if(changeCount(productId,result)){
				$(this).parent().find('input').val((stock));
				$.toastWeb({message:'库存不足'});
			}
		}
		$('#should').text(countTotalPrice());
		$('#totalPrice').text(countTotalPrice());
	});
	/**
	 * 数量加
	 */
	$('.c_add').click(function(){
		var count = parseInt($(this).parent().find('input').val());
		var result = count+1;
		var li = $(this).parents('li.list-item');
		var productId = $(li).attr('productId');
		var stock = parseInt($(li).find('.stock').text());
		if(stock > count){
			if(changeCount(productId,result)){
				$(this).parent().find('input').val(result);
			}
		}else{
			$(this).parent().find('input').val((stock));
			$.toastWeb({message:'库存不足'});
		}
		$('#should').text(countTotalPrice());
		$('#totalPrice').text(countTotalPrice());
	});
	/**
	 * 删除按钮
	 */
	$('.cardel').click(function(){
		var li = $(this).parents('li.list-item');
		var productId = $(li).attr('productId');
		if(removeProduct(productId)){
			$(this).parents('li').remove();
			$('#should').text(countTotalPrice());
			$('#totalPrice').text(countTotalPrice());
		}
		var lis = $('.news-list li.list-item');
		if(lis.length == 0){
			$('.news-list').html(setNullContent());
		}
	});
	/**
	 * 复选按钮
	 */
	$('.item-button-click .click').each(function(){
		$(this).click(function(){
			var ok = $(this).hasClass('click-ok');
			if(ok){
				$(this).removeClass('click-ok').addClass('click-cancel');
			}else{
				$(this).removeClass('click-cancel').addClass('click-ok');
			}
			if(checkClick()){
				$('.footer-button-click .click').addClass('click-ok');
			}else{
				$('.footer-button-click .click').addClass('click-cancel');
			}
			$('#should').text(countTotalPrice());
			$('#totalPrice').text(countTotalPrice());
		});
	});
	/**
	 * 检测是否全部全选
	 */
	function checkClick(){
		var flag = true;
		$('.footer-button-click .click').removeClass('click-ok').removeClass('click-cancel');
		$('.item-button-click .click').each(function(){
			var cancel = $(this).hasClass('click-cancel');
			if(cancel){
				flag = false;
				return;
			}
		});
		return flag;
	}
	/**
	 * 全选切换
	 */
	$('.footer-button-click').click(function(){
		var button = $(this).find('.click');
		var ok = $(button).hasClass('click-ok');
		if(ok){
			$(button).removeClass('click-ok').addClass('click-cancel');
			$('.item-button-click .click').removeClass('click-ok').addClass('click-cancel');
		}else{
			$(button).removeClass('click-cancel').addClass('click-ok');
			$('.item-button-click .click').removeClass('click-cancel').addClass('click-ok');
		}
		$('#should').text(countTotalPrice());
		$('#totalPrice').text(countTotalPrice());
	});
}
/**
 * 输入数量
 * @param _this
 * @param _stock
 */
function NumberInputKeyup(_this,_stock){
	$(_this).val(numberKeyup(_this,_stock));
	var li = $(_this).parents('li.list-item');
	var productId = $(li).attr('productId');
	var result = $(_this).val();
	if(changeCount(productId,result)){
		$('#should').text(countTotalPrice());
		$('#totalPrice').text(countTotalPrice());
	}
}
/**
 * 统计总金额
 * @returns
 */
function countTotalPrice(){
	var total = 0;
	$('.news-list li .click-ok').each(function(){
		var li = $(this).parents('li.list-item');
		var number = parseInt($(li).find('.c_number input').val());
		var price = parseFloat($(li).find('.unit-price').text());
		var ji = price * number;
		total += ji;
	});
	return changeTwoDecimal(parseFloat(total));
}

/**
 * 更改购物车中商品数量
 * @param productId
 * @param count
 */
function changeCount(productId,count){
	var mask;
	var flag = false;
	$.ajax({
	   type: "POST",
	   url: ctx + '/web/car/changeCount',
	   async : false,
	   beforeSend: function(){
		   mask = $.maskWeb();
	   },
	   data: {openid : $('#openid').val(), productId : productId, count : count},
	   success: function(data){
		   if(!data.success){
			   $.toastWeb({message:data.message});
		   }else{
			   flag = true;
		   }
	   },
	   complete:function(){
		   $.unmaskWeb(mask);
	   }
	});
	return flag;
}

/**
 * 立即下单按钮
 */
function submitGoods(){
	var ok = $('.item-button-click .click-ok');
	var cancel = $('.item-button-click .click-cancel');
	if(cancel.length > 0){
		if(ok.length == 0){
			$.toastWeb({message:'您还没有选择产品'});
			return;
		}
	}
	var lis = $('.news-list li.list-item');
	if(lis.length == 0){
		$.toastWeb({message:'您的购物车没有产品'});
		return;
	}
	
	
	var stockFlag = 0;
	var array = [];
	$('.item-button-click .click-ok').each(function(){
		var li = $(this).parents('li.list-item');
		var productId = $(li).attr('productId');
		var count = parseInt($(li).find('input').val());
		var stock = parseInt($(li).find('.stock').text());
		if(count > stock){
			stockFlag++;
		}
		var obj = new Object();
		obj.id = productId;
		obj.count = count;
		array.push(obj);
	});
	if(stockFlag > 0){
		$.toastWeb({message:'您有 '+stockFlag+' 件商品库存不足'});
		return;
	}
	var jsonText = JSON.stringify(array);
	$('#goods').val(jsonText);
	orderForm.submit();
}

/**
 * 删除购物车中商品
 */
function removeProduct(productId){
	var mask;
	var flag = false;
	$.ajax({
	   type: "POST",
	   url: ctx + '/web/car/removeProduct',
	   async : false,
	   beforeSend: function(){
		   mask = $.maskWeb();
	   },
	   data: {openid : $('#openid').val(), productId : productId},
	   success: function(data){
		   if(data.success){
			   flag = true;
		   }
		   $.toastWeb({message:data.message});
	   },
	   complete:function(){
		   $.unmaskWeb(mask);
	   }
	});
	return flag;
}

/**
 * 返回列表HTML
 */
function setCount(obj){
	var html = '<li class="list-item" productId="' + obj.product.id +'">'
		+ '<div class="item-button-click"><div class="click click-ok"></div></div>'
		+ '<div class="item-image-box">'
		+ 	'<a href="'+ ctx + '/web/product/detail?id=' + obj.product.id +'">'
		+ 		'<img src="' + ctx + obj.product.image1+'">'
		+ 	'</a>'
		+ '</div>'
		+ '<div class="item-info">'
		+ 	'<div class="title">'
		+ 		'<a href="'+ ctx + '/web/product/detail?id=' + obj.product.id +'">'+obj.product.productName+'</a>'
		+ 	'</div>'
		+ 	'<div class="price">'
		+   	'<div class="price_stock">库存：<span class="stock">'+obj.product.stock+'</span></div>'
		+   	'<div class="price_unit">单价：<b class="red">￥<span class="unit-price">'+ changeTwoDecimal(obj.buyPrice) +'</span></b></div>'
		+   '</div>'
		+ 	'<div class="number">'
		+		'<div style="float: left;">数量：</div>'
		+   	'<div class="number_tool" style="float: left;">'
		+			'<div class="c_d c_subtract"></div>'
		+				'<div class="c_d c_number">'
		+					'<input onblur="NumberInputKeyup(this,\''+obj.product.stock+'\')" onkeyup="NumberInputKeyup(this,\''+obj.product.stock+'\')" type="text" value="'+obj.buyCount+'" />'
		+				'</div>'
		+			'<div class="c_d c_add"></div>'
		+		'</div>'
		+		'<div class="right_del" align="right">'
		+			'<a class="button bg-red cardel" style="font-size: 14px;margin-left: 10px;">删除</a>'
		+ 		'</div>'
		+   '</div>'
		+ '</div>'
		+'<hr class="clear"></li>';
	return html;
}
function setNullContent(){
	var html = '<li><div style="padding-top: 30px;padding-bottom: 30px; text-align: center;">您的购物车空空如也~</div></li>';
	$('.count').before('<hr class="clear"/>');
	return html;
}